var fs = require('fs');
var path = require('path');
var sha1 = require('sha1');
var express = require('express');
var Ep=require('eventproxy');
var router = express.Router();

var UserModel = require('../models/users');
var checkNotLogin = require('../middlewares/check').checkNotLogin;

// GET /signup 注册页
router.get('/', checkNotLogin, function(req, res, next) {
    res.render('signup');
});

// POST /signup 用户注册
router.post('/', checkNotLogin, function(req, res, next) {
    var name = req.fields.name;
    var gender = req.fields.gender;
    var bio = req.fields.bio;
    var password = req.fields.password;
    var repassword = req.fields.repassword;

    // 校验参数
    var ep=new Ep();
    ep.on('signup_error',err=>{
        // 注册失败，异步删除上传的头像
        fs.unlink(req.files.avatar.path);
        req.flash('error', err);
        return res.redirect('/signup');
    });

    if (!(name.length >= 1 && name.length <= 10))return ep.emit('signup_error','名字请限制在 1-10 个字符');
    if (password.length < 6)return ep.emit('signup_error','密码至少 6 个字符');
    if (password !== repassword)return ep.emit('signup_error','两次输入密码不一致');
    if (['m', 'f', 'x'].indexOf(gender) === -1)return ep.emit('signup_error','性别只能是 m、f 或 x');
    if (!req.files.avatar.name)return ep.emit('signup_error','缺少头像');
    if (!(bio.length >= 1 && bio.length <= 30))return ep.emit('signup_error','个人简介请限制在 1-30 个字符');


    // 明文密码加密
    password = sha1(password);

    // 待写入数据库的用户信息
    var user = {
        name: name,
        password: password,
        gender: gender,
        bio: bio,
        avatar: req.files.avatar.path.split(path.sep).pop(),
        createTime:new Date
    };
    // 用户信息写入数据库
    UserModel.create(user)
        .then(function (result) {
            // 此 user 是插入 mongodb 后的值，包含 _id
            user = result;
            // 将用户信息存入 session
            delete user.password;
            req.session.user = user;
            // 写入 flash
            req.flash('success', '注册成功');
            // 跳转到首页
            res.redirect('/notify');
        })
        .catch(function (e) {
            // 注册失败，异步删除上传的头像
            fs.unlink(req.files.avatar.path);
            // 用户名被占用则跳回注册页，而不是错误页
            if (e.message.match('E11000 duplicate key')) {
                req.flash('error', '用户名已被占用');
                return res.redirect('/signup');
            }
            next(e);
        });
});

module.exports = router;